package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.controller.UniversityTemplates;
import com.globallogic.javatrainings.sergiiivanov.model.Building;
import com.globallogic.javatrainings.sergiiivanov.model.Const;
import com.globallogic.javatrainings.sergiiivanov.model.Course;
import com.globallogic.javatrainings.sergiiivanov.model.Lecture;
import com.globallogic.javatrainings.sergiiivanov.model.Personnel;
import com.globallogic.javatrainings.sergiiivanov.model.Student;
import com.globallogic.javatrainings.sergiiivanov.model.Teacher;
import com.globallogic.javatrainings.sergiiivanov.model.University;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;

public class RemovingDataTests {

    @Test
    public void testRemoveTeachers() throws UniversityFailure, UniversityWarning {
        University u = UniversityTemplates.universityTemplte1();
        for (Teacher t : u.teachers.getTeachers().copy()) {
            u.teachers.removeTeacher(t);
        }
        for (Course c : u.courses.getCourses().copy()) {
            assertNull(c.getTeacher());
        }
    }

    @Test
    public void testRemoveStudents() throws UniversityFailure, UniversityWarning {
        University u = UniversityTemplates.universityTemplte1();
        for (Student s : u.students.getStudents().copy()) {
            u.students.removeStudent(s);
        }
        assertEquals("Students are not removed", 0, u.students.countStudents());
        for (Course c : u.courses.getCourses().copy()) {
            assertEquals("Enrolled students are not removed " + c.getName(), 0, c.countStudents());
            assertEquals("Scores are not removed from course " + c.getName(), 0, c.getScoreTable().length);
        }
        for (Teacher t : u.teachers.getTeachers().copy()) {
            assertEquals("Scores are not removed from teacher " + t.getName(), 0, t.getScoreTable().length);
        }
    }

    @Test
    public void testRemoveCourses() throws UniversityFailure, UniversityWarning {
        University u = UniversityTemplates.universityTemplte1();
        for (Course c : u.courses.getCourses().copy()) {
            u.courses.removeCourse(c);
        }
        for (Student s : u.students.getStudents().copy()) {
            assertEquals("Student still linked to course " + s.getName(), 0, s.getCoursesList().size());
        }
        for (Teacher t : u.teachers.getTeachers().copy()) {
            assertEquals("Teacher still linked to course " + t.getName(), 0, t.getCourses().size());
        }
    }

    @Test
    public void testRemoveBuildings() throws UniversityFailure, UniversityWarning {
        University u = UniversityTemplates.universityTemplte1();
        for (Building b : u.buildings.getBuildings().copy()) {
            u.buildings.removeBuilding(b);
        }
        for (Course c : u.courses.getCourses().copy()) {
            for (Lecture l : c.getLectures().copy()) {
                assertEquals("Lecture still appointed to room " + l.getName(), Const.STRING_EMPTY, l.getRoomName());
            }
        }
        for (Personnel p : u.personnel.getPersonnel().copy()) {
            assertEquals("Person still have assigned rooms " + p.getName(), 0, p.getAssignedRooms().size());
        }
    }
}
